package com.paomi.client.activity.person;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.person.View.SettingView;
import com.paomi.client.activity.person.model.loginOutEntity;
import com.paomi.client.activity.person.presenter.SettingPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.UserUtils;
import com.paomi.client.utils.CleanDataUtils;
import com.paomi.client.utils.IntentUtils;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseMvpActivity<SettingView, SettingPre> implements SettingView {
    @BindView(R.id.tv_arrow_permission)
    TextView tv_arrow_permission;
    @BindView(R.id.tv_arrow_cache)
    TextView tv_arrow_cache;
    @BindView(R.id.tv_arrow_about)
    TextView tv_arrow_about;
    @BindView(R.id.tv_arrow_opinion)
    TextView tv_arrow_opinion;
    @BindView(R.id.tv_go_out)
    TextView tv_go_out;


    //显示
    @BindView(R.id.tv_cache)
    TextView tv_cache;

    @BindView(R.id.button_goback)
    TextView button_goback;

    Dialog cachedialog;

    @NonNull
    @Override
    public SettingPre createPresenter() {
        return new SettingPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.setting_get);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @OnClick(R.id.ll_permission)
    void topermission() {//跳转权限页面
        new IntentUtils(this).jumpPermissionPage();
    }

    @OnClick(R.id.ll_about)
    void aboutUs() {
        Intent i = new Intent(this, AboutUsActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.ll_opinion)
    void vertical() {
        Intent i = new Intent(this, OpinionActivity.class);
        startActivity(i);
    }

    @OnClick(R.id.ll_cache)
    void cache() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.layout_clear_cache, null);
        builder.setView(view);
        builder.setCancelable(true);
        TextView btn_clear = view.findViewById(R.id.btn_clear);
        TextView btn_success = view.findViewById(R.id.btn_success);
        cachedialog = builder.create();
        cachedialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        cachedialog.show();

        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cachedialog.dismiss();
            }
        });
        btn_success.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    cachedialog.dismiss();
                    CleanDataUtils.clearAllCache(SettingActivity.this);
                    ToastUtil.getToastEmail().ToastShow(SettingActivity.this, null,
                            1, R.layout.toast_view, getResources().getString(R.string.cache_msg));
                    tv_cache.setText("0.00K");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (UserUtils.getInstance().getUserInfo1() != null) {
            tv_go_out.setVisibility(View.VISIBLE);
        } else {
            tv_go_out.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.tv_go_out)
    void loginOut() {
        getPresenter().loginOut();
    }

    @Override
    protected void initEvent() {
        initTextType();

        try {
            String totalCacheSize = CleanDataUtils.getTotalCacheSize(this);
            tv_cache.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void initTextType() {
        tv_arrow_permission.setTypeface(iconfont);
        tv_arrow_cache.setTypeface(iconfont);
        tv_arrow_about.setTypeface(iconfont);
        tv_arrow_opinion.setTypeface(iconfont);
        button_goback.setTypeface(iconfont);
    }

    @Override
    public void showOut(loginOutEntity json) {
        if (json.returnCode.equals("0000")) {
            if (json.getReturnData().equals("true")) {
                UserUtils.getInstance().clearUser();
                finish();
            }
        } else {
            Util.toast(this, json.returnMessage);
        }
    }
}
